package com.trkj.service.wdm;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wdm.BonusAdministrationDAO;
import com.trkj.dao.wdm.BonusPenaltypeDAO;
import com.trkj.dao.wdm.StaffInformationDAO;
import com.trkj.pojo.BonusAdministration;
import com.trkj.pojo.BonusPenaltype;
import com.trkj.pojo.DefinitionDept;
import com.trkj.pojo.Duty;
import com.trkj.pojo.JobStatus;
import com.trkj.pojo.ProfessionalTitle;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.vo.BonusAdministrationVO;
import com.trkj.vo.StafffinalVO;

@Service
@Transactional(rollbackFor = Exception.class)
public class BonusAdministrationService {
	@Autowired
	private BonusAdministrationDAO bonusadministrationdao;
	@Autowired
	private BonusPenaltypeDAO bonuspenaltypedao;
	@Autowired
	private StaffInformationDAO staffinformationdao;
	
	/**
	 * 分页查询所有
	 * @return
	 */
	public List<BonusAdministrationVO> queryByPage(int page,int rows){
		List<Object[]> list =bonusadministrationdao.queryByNaPage(new PageRequest(page-1, rows));
		List<BonusAdministrationVO> listvo = new ArrayList<>();
		if(list!=null){
			for(Object[] objs:list){
				BonusAdministration  b = (BonusAdministration) objs[0];
				BonusPenaltype p = (BonusPenaltype) objs[1];
				StaffInformation s =(StaffInformation) objs[2];
				
				BonusAdministrationVO bonusvo = new BonusAdministrationVO();
				bonusvo.setBpadId(b.getBpadId());
				bonusvo.setBpadMoney(b.getBpadMoney());
				bonusvo.setBpadTime(b.getBpadTime());
				bonusvo.setBpadRemarks(b.getBpadRemarks());
				bonusvo.setBptId(p.getBptId());
				bonusvo.setBptName(p.getBptName());
				bonusvo.setStainId(s.getStainId());
				bonusvo.setStainName(s.getStainName());
				listvo.add(bonusvo);
			}
		}
		return listvo;
	}
	
	/**
	 * 获取总记录条数
	 * @return
	 */
	public long queryByPageCount(){
		return bonusadministrationdao.count();
	}
	/**
	 * 新增
	 * @param b
	 * @param p
	 * @param s
	 */
	public void insert(BonusAdministrationVO bs){
		try {
			BonusPenaltype bonus = bonuspenaltypedao.findOne(bs.getBptId());
			StaffInformation staff =staffinformationdao.findOne(bs.getStainId());
			BonusAdministration ba = new BonusAdministration();
			ba.setBonusPenaltype(bonus);
			ba.setStaffInformation(staff);
			ba.setBpadMoney(bs.getBpadMoney());
			ba.setBpadRemarks(bs.getBpadRemarks());
			ba.setBpadTime(bs.getBpadTime());
			bonusadministrationdao.save(ba);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 编辑
	 * @param bs
	 */
	public void updateAdministration(BonusAdministrationVO bs){
		try {
			BonusPenaltype bonus = bonuspenaltypedao.findOne(bs.getBptId());
			StaffInformation staff =staffinformationdao.findOne(bs.getStainId());
			BonusAdministration ba =bonusadministrationdao.findOne(bs.getBpadId());
			ba.setBonusPenaltype(bonus);
			ba.setStaffInformation(staff);
			ba.setBpadMoney(bs.getBpadMoney());
			ba.setBpadRemarks(bs.getBpadRemarks());
			ba.setBpadTime(bs.getBpadTime());
			bonusadministrationdao.save(ba);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 根据id查
	 * @param bpadId
	 * @return
	 */
	public BonusAdministration update(Long bpadId){
		try {
			BonusAdministration ba =bonusadministrationdao.findOne(bpadId);
			return ba;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
	
	/**
	 * 删除
	 */
	public void delete(Long id){
		try {
			bonusadministrationdao.delete(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 分页查询所有
	 * @param page
	 * @param size
	 * @return
	 */
	public List<StafffinalVO> findAll(int page,int rows){
		List<StafffinalVO> listvo = new ArrayList<>();
		List<Object[]> obj = bonusadministrationdao.queryStaffAll(new PageRequest(page-1,rows));
		if(obj!=null){
			for (Object[] o : obj) {
				StafffinalVO vo = new StafffinalVO();
				StaffInformation s = (StaffInformation) o[0];
				vo.setStainId(s.getStainId());
				vo.setStainName(s.getStainName());
				vo.setStainSrc(s.getStainSrc());
				
				DefinitionDept d = (DefinitionDept) o[1];
				vo.setDefideName(d.getDefideName());
				
				ProfessionalTitle p = (ProfessionalTitle) o[2];
				vo.setZcName(p.getZcName());
				
				Duty duty = (Duty) o[3];
				vo.setDutyName(duty.getDutyName());
				
				listvo.add(vo);
			}
		}
		return listvo;
	}
	/**
	 * 查询单条员工
	 * @param page
	 * @param size
	 * @return
	 */
	public StafffinalVO LikefindOne(Long stainid){
		StafffinalVO vo = new StafffinalVO();
		List<Object[]> obj = bonusadministrationdao.LikeStaffOne(stainid);
		if(obj!=null){
			for (Object[] o : obj) {
				StaffInformation s = (StaffInformation) o[0];
				vo.setStainId(s.getStainId());
				vo.setStainName(s.getStainName());
				vo.setStainSrc(s.getStainSrc());
				vo.setStainEstimatedate(s.getStainEstimatedate());
				DefinitionDept d = (DefinitionDept) o[1];
				vo.setDefideName(d.getDefideName());
				ProfessionalTitle p = (ProfessionalTitle) o[2];
				vo.setZcName(p.getZcName());
				Duty duty = (Duty) o[3];
				vo.setDutyName(duty.getDutyName());
				JobStatus jobs = (JobStatus) o[4];
				vo.setJobsName(jobs.getJobsName());
			}
		}
		return vo;
	}
}
